home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODF-Interest Archive / September 96 / Re Creating a resource from a < prev    next >
Encoding:
Internet Message Format  |  1996-09-19  |  5.4 KB  |  [TEXT/ttxt]

  1. Subject:     Re: Creating a resource from a stream
  2. Sent:        9/4/96 12:15 PM
  3. Received:    9/4/96 12:15 PM
  4. From:        motion@nbn.com (Arni McKinley)
  5. Reply-To:    ODF-Interest@CILabs.ORG
  6. To:          OpenDoc Development Framework Discussion List
  7.  
  8.  
  9. >I have implemented a Save As dialog similar to that of Cyberdog, and can
  10. >now output TEXT files. Now I would like to output stylized text files. Is
  11. >there a way to create a 'styl' resource from a stream?
  12. >
  13. >Ken Boyer
  14. >Digital Harbor
  15.  
  16. ____________
  17.  
  18. Hi Ken,
  19.  
  20. To stream styled text, write out the text first as a FW_CString, then call
  21. GetTEScrap() below using the TEHandle. This returns a handle to a
  22. StScrpHandle. Write this handle to the stream.  Here are some tuilities:
  23.  
  24. //--------------------------------------------------------------------------
  25. --------------
  26. //      ::GetTEScrap
  27. //
  28. //              Get the style info of a TERec (called the "scrap"). This is
  29. a copy
  30. //--------------------------------------------------------------------------
  31. --------------
  32. StScrpHandle    GetTEScrap( TEHandle macTEHndl )
  33. {
  34.         if( macTEHndl == NULL )
  35.                 return NULL;
  36.  
  37.         // Select all of the text first
  38.         short oldStart = (**macTEHndl).selStart;
  39.         (**macTEHndl).selStart = 0;
  40.         short oldEnd = (**macTEHndl).selEnd;
  41.         (**macTEHndl).selEnd = 32767;
  42.  
  43.         // Get the StScrpHandle
  44.         StScrpHandle hStScrap = ::TEGetStyleScrapHandle( macTEHndl );
  45.  
  46.         // Reset the old selection points
  47.         (**macTEHndl).selStart = oldStart;
  48.         (**macTEHndl).selEnd = oldEnd;
  49.  
  50.         return hStScrap;
  51. }
  52.  
  53.  
  54. //--------------------------------------------------------------------------
  55. --------------
  56. //      ::SetTEScrap
  57. //
  58. //              Set the style info of a TERec (called the "scrap") into the
  59. given TERec
  60. //--------------------------------------------------------------------------
  61. --------------
  62. void    SetTEScrap( FW_CString& text, StScrpHandle hStHndl, TEHandle macTEHndl )
  63. {
  64.         if( macTEHndl == NULL )
  65.                 return;
  66.  
  67.         ::TEStyleInsert( text.RevealBuffer(), text.GetByteLength(),
  68. hStHndl, macTEHndl );
  69. }
  70.  
  71. //--------------------------------------------------------------------------
  72. --------------
  73. //      ::WriteTEScrapDirect
  74. //
  75. //              Write the style info of a TERec (called the "scrap") to the
  76. given archive
  77. //--------------------------------------------------------------------------
  78. --------------
  79. void    WriteTEScrapDirect( FW_CWritableStream& archive, StScrpHandle hStScrap )
  80. {
  81.         FW_ASSERT( hStScrap != NULL );
  82.  
  83.         archive << (FW_Boolean) TRUE;  // The record exists
  84.  
  85.         // Save the size of the scrap so we can read it back in
  86.         unsigned long scrapLen = FW_CMemoryManager::GetSystemHandleSize(
  87. (FW_PlatformHandle) hStScrap );
  88.         archive << scrapLen;
  89.  
  90.         FW_CAcquireLockedSystemHandle locked (( FW_PlatformHandle) hStScrap );
  91.         archive.Write( locked.GetPointer(), scrapLen );
  92.  
  93.         // Don't delete!!
  94. }
  95.  
  96. //--------------------------------------------------------------------------
  97. --------------
  98. //      ::WriteTEScrap
  99. //
  100. //              Write the style info of a TERec (called the "scrap") to the
  101. given archive
  102. //--------------------------------------------------------------------------
  103. --------------
  104. void    WriteTEScrap( FW_CWritableStream& archive, TEHandle macTEHndl )
  105. {
  106.         FW_ASSERT( macTEHndl != NULL );
  107.  
  108.         // Get the STScrpHandle
  109.         StScrpHandle hStScrap = ::GetTEScrap( macTEHndl );
  110.         // Now save the StScrpRec
  111.         if( hStScrap == NULL )
  112.                 archive << (FW_Boolean) FALSE;
  113.         else
  114.         {
  115.                 archive << (FW_Boolean) TRUE;  // The record exists
  116.  
  117.                 // Save the size of the scrap so we can read it back in
  118.                 unsigned long scrapLen =
  119. FW_CMemoryManager::GetSystemHandleSize( (FW_PlatformHandle) hStScrap );
  120.                 archive << scrapLen;
  121.  
  122.                 FW_CAcquireLockedSystemHandle locked (( FW_PlatformHandle)
  123. hStScrap );
  124.                 archive.Write( locked.GetPointer(), scrapLen );
  125.  
  126.                 // Cleanup
  127.                 FW_CMemoryManager::FreeSystemHandle( (FW_PlatformHandle)
  128. hStScrap );
  129.         }
  130. }
  131.  
  132. //--------------------------------------------------------------------------
  133. --------------
  134. //      ::ReadTEScrap
  135. //
  136. //              Read the style info of a TERec (called the "scrap") from
  137. the given archive
  138. //--------------------------------------------------------------------------
  139. --------------
  140. StScrpHandle    ReadTEScrap( FW_CReadableStream& archive )
  141. {
  142.         FW_Boolean recordExists;
  143.         archive >> recordExists;
  144.         if( recordExists == FALSE )
  145.                 return NULL;
  146.  
  147.         // Get the length of the scrap handle
  148.         unsigned long scrapLen;
  149.         archive >> scrapLen;
  150.  
  151.         StScrpHandle hStScrap = (StScrpHandle)
  152. FW_CMemoryManager::AllocateSystemHandle( scrapLen );
  153.         FW_ASSERT( hStScrap );
  154.         FW_TRY
  155.         {
  156.                 FW_CAcquireLockedSystemHandle locked (( FW_PlatformHandle)
  157. hStScrap );
  158.                 archive.Read( locked.GetPointer(), scrapLen );
  159.         }
  160.         FW_CATCH_BEGIN
  161.         FW_CATCH_EVERYTHING ()
  162.         {
  163.                 FW_CMemoryManager::FreeSystemHandle( (FW_PlatformHandle)
  164. hStScrap );
  165.                 FW_THROW_SAME ();
  166.         }
  167.         FW_CATCH_END
  168.  
  169.         return hStScrap;
  170. }
  171.  
  172. Arni F. McKinley
  173. President
  174. MetaMind Software, Inc.
  175. Mill Valley, CA 94941
  176. motion@nbn.com
  177. www.nbn.com/people/minds
  178. _______________________________________